<?php

use yii\db\Migration;

/**
 * Class m181129_072007_create_card_replace
 */
class m181129_072007_create_card_replace extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%card_replace}}', [
            'id' => $this->primaryKey(),
            'from_card_id' => $this->integer()->notNull()->comment('旧卡编号'),
            'to_card_id' => $this->integer()->notNull()->comment('新卡编号'),
            'require_sub_uid' => $this->integer()->notNull()->comment('申请人子用户编号'),
            'status' => $this->integer()->notNull()->comment('状态'),
            'create_time' => $this->integer()->notNull()->comment('创建时间'),
            'verify_time' => $this->integer()->comment('审核时间'),
        ]);
        $this->createIndex('fk_card_replace_card1_idx', '{{%card_replace}}', ['from_card_id']);
        $this->addForeignKey('fk_card_replace_card1', '{{%card_replace}}', ['from_card_id'], '{{%card}}', ['id']);
        $this->createIndex('fk_card_replace_card2_idx', '{{%card_replace}}', ['to_card_id']);
        $this->addForeignKey('fk_card_replace_card2', '{{%card_replace}}', ['to_card_id'], '{{%card}}', ['id']);
        $this->createIndex('fk_card_replace_sub_user1_idx', '{{%card_replace}}', ['require_sub_uid']);
        $this->addForeignKey('fk_card_replace_sub_user1_idx', '{{%card_replace}}', ['require_sub_uid'], '{{%sub_user}}', ['id']);
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['card_replace_status', 1, '待审核'],
            ['card_replace_status', 2, '已完成'],
            ['card_replace_status', 9, '拒绝'],
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->delete('{{%key_map}}', ['t' => 'card_replace_status']);
        $this->dropTable('{{%card_replace}}');
    }
}
